home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / kcl / akcl / akcl1615.lha / mp / mp_mulul3.c < prev    next >
C/C++ Source or Header  |  1991-02-12  |  953b  |  50 lines

  1.  
  2.  
  3. #include "include.h"
  4. #include "arith.h"
  5.  
  6. /* ulong a,b,y;
  7.    (y = mulul3(a,b,&hiremainder), hiremainder:y == a*b) is TRUE.
  8. */   
  9. #ifdef USE_WORD_MULUL3
  10.  
  11. int mulul3(x,y,hiremainder)
  12.      ulong x,y,*hiremainder;
  13. {
  14.   ulong xlo,xhi,ylo,yhi;
  15.   ulong z; TEMPVARS
  16.  
  17.   xlo=x&65535;xhi=x>>16;ylo=y&65535;yhi=y>>16;
  18.   z=addll(xlo*yhi,xhi*ylo);
  19.   *hiremainder=(overflow)?xhi*yhi+65536+(z>>16):xhi*yhi+(z>>16);
  20.   z=addll(xlo*ylo,(z<<16));*hiremainder+=overflow;
  21.   return z;
  22. }
  23.  
  24. #else
  25. ulong
  26. mulul3(a,b,h)
  27. unsigned int a,b, *h;
  28. {unsigned int temph,templ,ah,al,i;
  29.  ah=0;
  30.  al=0;
  31.  /* in case the shift by 32 does not zero an unsigned int..
  32.     we separate out the first step.*/
  33.  {if (b & 1)
  34.     {temph=0;templ=a;
  35.      lladd(temph,templ,ah,al);}
  36.     /*    printf("\n%d b=%d a=%d (%d:%d)",i,b,a,ah,al); */
  37.     b=b>>1;
  38.   }
  39.  i=1;
  40.  while(b)
  41.    {if (b & 1)
  42.       {llshift(a,i,temph,templ);
  43.        lladd(temph,templ,ah,al);}
  44.       i++;b=b>>1;
  45.     }
  46.  *h=ah;
  47.  return al;
  48. }
  49. #endif
  50.